/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * @author Administrator
 * Date: 2010-7-6 上午09:44:51
 */
package com.ccxe.manage.dao.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ccxe.manage.util.help.DaoHelper;

/**
 * IM_MENU_ROLE 表 dao
 * 
 * @author wangyx
 * 
 */
public class IM_Menu_Role_DaoImpl {

	private static final Logger logger = LoggerFactory.getLogger(IM_Menu_Role_DaoImpl.class);
	private DaoHelper dHelper = new DaoHelper();

	/**
	 * wangyx:由roleId获取权限列表
	 * 
	 * @param roleId
	 * @return
	 * @throws Exception
	 */
	public List<Map<?, ?>> queryListByRoleID(String roleId) throws Exception {
		return dHelper.getListBySql(getQueryListByRoleIdSql(roleId));
	}
	
	public List<Map<?, ?>> getTreeListByRoleId(String roleId) throws Exception{
		return dHelper.getListBySql(getTreeListByRoleIdSql(roleId));
	}
	
	/**
	 * 获取用户的权限
	 * @param username
	 * @return
	 * @throws Exception
	 */
	public List<Map<?, ?>> getTreeListByUsername(String username) throws Exception{
		return dHelper.getListBySql(getTreeListByUsernameSql(username));
	}

	/**
	 * 插入一条数据
	 * 
	 * @param roleId
	 * @param menuId
	 * @throws Exception
	 */
	public void insertOne(String roleId, String menuId) throws Exception {
		dHelper.insertOrUpdateOrDelete(getInsertOneSql(), new String[] {
				roleId.trim(), menuId.trim() });
	}

	public String getQueryListByRoleIdSql(String roleid) {
		return " select i. c_MENU_ROLE i,C_ROLE r" + roleIdWhere(roleid);
	}

	public String getTreeListByRoleIdSql(String roleId) {
		return " select t.menu_id id,t.parent_id pid ,t.menu_nm name ,' ' url  from C_MENU_ROLE i,MENU_ITEM_TMP  t where i.role_id = "
				+ returnRoleId(roleId) + " and  i.menu_id = t.menu_id ";
	}
	public String getTreeListByUsernameSql(String username) {
		String sql = " select t.menu_id id,t.parent_id pid ,t.menu_nm name ,t.menu_url url " 
			+"from C_MENU_ROLE i,MENU_ITEM_TMP  t where i.role_id = "
			+"(select role_id from c_user_role where user_id = '"+username +"')"
			+"and  i.menu_id = t.menu_id ";
logger.info("IM_Menu_Role_DaoImpl SQL :"+sql);		
		return sql;
	}

	public String getDeleteByRoleIdSql(String roleid) {
		return " delete " + fromString() + roleIdWhere(roleid);
	}

	public String getDeleteByRoleIdSql() {
		return " delete " + fromString() + "where " + getRoleIdWhere_Q_Mark();
	}

	/**
	 * 插入操作，预声明sql
	 * 
	 * @return
	 */
	public String getInsertOneSql() {
		return " insert into c_MENU_ROLE values(?,?) ";
	}

	private String fromString() {
		return " from c_MENU_ROLE ";
	}

	private String roleIdWhere(String roleId) {
		return "where ROLE_ID =" + returnRoleId(roleId);
	}

	private Object returnRoleId(String roleId) {
		String sId = roleId.toString();
		sId = "'" + sId + "'";
		return sId;
	}

	public String getRoleIdWhere_Q_Mark() {
		return " ROLE_ID = ?";
	}
}
